using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Reflection;
using System.IO;
using System.Data.SqlClient;

namespace TST.SharePointObjects
{
    public class SharePointObjectsMenuInstaller : SPFeatureReceiver
    {
        /// <summary>
        /// Occurs after a Feature is installed.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        /// <summary>
        /// Occurs when a Feature is uninstalled.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

        /// <summary>
        /// Occurs after a Feature is activated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                if (properties.Feature.Parent is SPSite)
                {
                    RegisterBreadCrumbNodes((SPSite)properties.Feature.Parent);
                }
            }
            catch (Exception ex)
            {
                TraceProvider.WriteError(string.Format("Error installing SharePoint Objects timerjob: {0}.", ex.Message), "IndexerJobInstaller.FeatureActivated");
                throw ex;
            }
        }

        private void RegisterBreadCrumbNodes(SPSite site)
        {
            if (SPAdministrationWebApplication.Local.Id == site.WebApplication.Id)
            {
                SPWebService.AdministrationService.ApplyApplicationContentToLocalServer();
            }
        }

        /// <summary>
        /// Occurs when a Feature is deactivated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
        }
    }
    
}
